package com.mxt.moviedemo.controller;


import com.mxt.moviedemo.entity.Actors;
import com.mxt.moviedemo.entity.Directors;
import com.mxt.moviedemo.service.DirectorsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("director")
public class DirectorsController {
    private static final Logger logger = LoggerFactory.getLogger(DirectorsController.class);
    @Autowired
    private DirectorsService directorsService;
    @GetMapping("/searchDirector")
    public Map<String, Object> searchDirector(@RequestParam("dname") String dname) {
        logger.info("获取导演信息，id：{}", dname);
        Map<String, Object> result = new HashMap<>();

        try {
            List<Directors> directors=directorsService.searchDirectorsByName(dname);
            if (directors != null) {
                result.put("success", true);
                result.put("directors", directors);
            } else {
                result.put("success", false);
                result.put("error", "未找到该导演");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", "查询导演信息失败：" + e.getMessage());
        }
        return result;
    }
    @GetMapping("/infoDirector")
    public Map<String, Object> getDirectorInfo(@RequestParam("did") int did) {
        logger.info("获取导演信息，id：{}", did);
        Map<String, Object> result = new HashMap<>();

        try {
            Directors directors = directorsService.getDirectorById(did);
            if (directors != null) {
                result.put("success", true);
                result.put("directors", directors);
            } else {
                result.put("success", false);
                result.put("error", "未找到该导演");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", "查询导演信息失败：" + e.getMessage());
        }
        return result;
    }

    // 新增订票
    @PostMapping("addDirector")
    public Map<String, Object> addDirector(@RequestBody Directors directors) {
        Map<String, Object> result = new HashMap<>();
        try {
            int rows = directorsService.addDirectors(directors);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "导演信息添加成功");
            } else {
                result.put("success", false);
                result.put("message", "演员信息添加失败");
            }
        } catch (Exception e) {
            logger.error("添加演员信息错误: ", e);
            result.put("success", false);
            result.put("message", "演员信息添加失败：" + e.getMessage());
        }
        return result;
    }

    @PutMapping("updateDirector")
    public Map<String, Object> updateDirector(@RequestBody Directors directors) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (directorsService.updateDirectors(directors)) {
                result.put("success", true);
                result.put("message", "导演信息修改成功");
            } else {
                result.put("success", false);
                result.put("message", "导演信息修改失败");
            }
        } catch (Exception e) {
            logger.error("导演信息错误: ", e);
            result.put("success", false);
            result.put("message", "导演信息修改失败：" + e.getMessage());
        }
        return result;
    }
    @DeleteMapping("deleteDirector/{did}")
    public Map<String, Object> deleteDirector(@PathVariable int did) {
        Map<String, Object> result = new HashMap<>();
        try {
            int rows = directorsService.deleteDirectors(did);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "导演信息删除成功");
            } else {
                result.put("success", false);
                result.put("message", "导演信息删除失败");
            }
        } catch (Exception e) {
            logger.error("删除导演信息错误: ", e);
            result.put("success", false);
            result.put("message", "导演信息删除失败：" + e.getMessage());
        }
        return result;
    }
}
